<?php include("IncAuthentication.php")?>
	
<table width="97%">
<?php
mysql_query("SET character_set_results=utf8");
if(isset($_REQUEST['btnDetail']))
{
	$MaHD = $_REQUEST['btnDetail'];
	$sql = "SELECT * FROM HoaDon WHERE MaHD=$MaHD";
	$rows = mysql_query($sql);
	$row = mysql_fetch_array($rows);
	
	$NgayDat = strtotime($row['NgayDat']);
	$NgayCan = strtotime($row['NgayCan']);
	$NgayGiao = strtotime($row['NgayGiao']);
	$HoTen = $row['HoTen'];
	$DiaChi = $row['DiaChi'];
	$CachThanhToan = $row['CachThanhToan'];
	$CachVanChuyen = $row['CachVanChuyen'];
	$PhiVanChuyen = $row['PhiVanChuyen'];
	$TrangThai = $row['TrangThai'];
	$GhiChu = $row['GhiChu'];
?>
  <tr>
    <td><fieldset>
      <legend>The selected order</legend>
      <table width="100%" border="0" cellpadding="5" cellspacing="0" style="text-align:left;">
        <tr bgcolor="#FFFFCC">
          <td><table border="0" align="center" cellpadding="3" cellspacing="0">
            <tr>
              <td>Order Date</td>
              <td>:</td>
              <td><?php echo date('F d, Y', $NgayDat)?></td>
            </tr>
            <tr>
              <td>Required Date</td>
              <td>:</td>
              <td><?php echo date('F d, Y', $NgayCan)?></td>
              </tr>
            <tr>
              <td valign="top">Shipped Date</td>
              <td valign="top">:</td>
              <td><?php echo date('F d, Y', $NgayGiao)?></td>
            </tr>
            <tr>
              <td>Recipient's Name</td>
              <td>:</td>
              <td><?php echo $HoTen?></td>
              </tr>
            <tr>
              <td>Recipient's Address</td>
              <td>:</td>
              <td><?php echo $DiaChi?></td>
              </tr>
            <tr>
              <td>Payment Method</td>
              <td>:</td>
              <td><?php echo $CachThanhToan?></td>
              </tr>
            <tr>
              <td><p>Shipping Method</p></td>
              <td>:</td>
              <td><?php echo $CachVanChuyen?></td>
              </tr>
            <tr>
              <td>Freight</td>
              <td>:</td>
              <td><?php echo $PhiVanChuyen?></td>
              </tr>
            <tr>
              <td valign="top">Order Status</td>
              <td valign="top">:</td>
              <td><?php echo $TrangThai?></td>
            </tr>
            <tr>
              <td valign="top">Notes</td>
              <td valign="top">:</td>
              <td><?php echo $GhiChu?></td>
              </tr>
            </table></td>
          </tr>
        <tr>
          <td>
            <table class="art-article" style="width:100%">
              <tr bgcolor="#FFFFCC">
                <th>Id</th>
                <th>Name</th>
                <th>Price</th>
                <th>Discount</th>
                <th>Quantity</th>
                <th>Amount</th>
                </tr>
		<?php
            $sql = "SELECT ct.*, hh.TenHH FROM ChiTietHD ct 
                    JOIN HangHoa hh ON hh.MaHH=ct.MaHH 
                    WHERE MaHD=$MaHD";  
			$rows = mysql_query($sql); 
            $TongTien = 0;
            while($row = mysql_fetch_array($rows))
            {
                $MaHH = $row['MaHH'];
                $TenHH = $row['TenHH'];
                $DonGia = $row['DonGia'];
                $GiamGia = $row['GiamGia']*100;
                $SoLuong = $row['SoLuong'];
                $ThanhTien = $DonGia*$SoLuong*(1-$GiamGia);
                $TongTien += $ThanhTien;
        ?>
                <tr bgcolor="#E29A9A" style="color:#FFF; font-weight:bold; text-align:center">
                    <td><?php echo $MaHH?></td>
                    <td><?php echo $TenHH?></td>
                    <td>$<?php echo $DonGia?></td>
                    <td><?php echo $GiamGia?>%</td>
                    <td><?php echo $SoLuong?></td>
                    <td>$<?php echo $ThanhTien?></td>
                </tr>
        <?php
            }
        ?>
              <tr>
                <th colspan="5" style="text-align:right">Total amount: </th>
                <th align="left">$<?php echo $TongTien?></th>
                </tr>
              </table>
            </td>
          </tr>
      </table>
    </fieldset></td>
  </tr>
<?php
} // isset($_REQUEST['btnDetail'])
?>
  <tr>
    <td><fieldset>
      <legend>List of orders</legend>
      <table class="art-article" style="width:100%">
        <tr bgcolor="#FFFFCC">
          <th width="17%">Order No</th>
          <th width="25%">Date</th>
          <th width="45%">Address</th>
          <th width="13%">&nbsp;</th>
        </tr>
<?php
$MaKH = $_SESSION['KhachHang']['MaKH'];
$sql = "SELECT * FROM HoaDon WHERE MaKH='$MaKH' ORDER BY MaHD DESC";
$rows = mysql_query($sql);
while($row = mysql_fetch_array($rows))
{
	$MaHD = $row['MaHD'];
	$NgayDat = strtotime($row['NgayDat']);
	$DiaChi = $row['DiaChi'];
?>
        <tr>
          <td><?php echo $MaHD ?></td>
          <td><?php echo date("d/m/Y", $NgayDat) ?></td>
          <td><?php echo $DiaChi ?></td>
          <td><a href="?p=PurchasedOrders&btnDetail=<?php echo $MaHD?>">Detail</a></td>
          </tr>
<?php
}
?>
      </table>
    </fieldset></td>
  </tr>
</table>